import {createRouter, createWebHashHistory} from 'vue-router'
import {
  ElMessage
} from 'element-plus'
const viewport = {
  content: 'width=device-width, initial-scale=1.0, user-scalable=no',
}

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/previewPage',
      name: 'previewPage',
      component: () => import('@/views/previewPage/index.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),
    },
    {
      path: '/workspace',
      name: 'workspace',
      component: () => import('@/views/workspace/WorkSpace.vue'),
      children: [
        {
          path: "/organizationList",
          name: "organizationList",
          component: () => import("@/views/organizationList/index.vue"),
          meta: {title: '蜀丰科技 | 部门管理', viewport: viewport}
        },
        {
          path: "/userList",
          name: "userList",
          component: () => import("@/views/userList/index.vue"),
          meta: {title: '蜀丰科技 | 用户管理', viewport: viewport}
        },
        {
          path: "/fileManage",
          name: "fileManage",
          component: () => import("@/views/fileManage/index.vue"),
          meta: {title: '蜀丰科技 | 文件共享', viewport: viewport}
        },
        {
          path: 'forms',
          name: 'forms',
          component: () => import('@/views/workspace/oa/FromsApp.vue'),
          meta: { title: '蜀丰科技 | OA审批', viewport: viewport },
        },
        {
          path: 'submit',
          name: 'submit',
          component: () => import('@/views/workspace/oa/MySubmit.vue'),
          meta: { title: '蜀丰科技 | 我发起的', viewport: viewport },
        },
        {
          path: 'cc',
          name: 'cc',
          component: () => import('@/views/workspace/oa/CcMe.vue'),
          meta: { title: '蜀丰科技 | 抄送我的', viewport: viewport },
        },
        {
          path: 'unfinished',
          name: 'unfinished',
          component: () => import('@/views/workspace/oa/UnFinished.vue'),
          meta: { title: '蜀丰科技 | 未完成的', viewport: viewport },
        },
        {
          path: 'finished',
          name: 'finished',
          component: () => import('@/views/workspace/oa/Finished.vue'),
          meta: { title: '蜀丰科技 | 未完成的', viewport: viewport },
        },
        {
          path: 'instances',
          name: 'instances',
          component: () => import('@/views/admin/ProcessInstanceManage.vue'),
          meta: { title: '蜀丰科技 | 数据管理', viewport: viewport },
        },
        {
          path: 'formsPanel',
          name: 'formsPanel',
          component: () => import('@/views/admin/FormsPanel.vue'),
          meta: { title: '蜀丰科技 | 表单列表', viewport: viewport },
        },
        {
          path: "subProcess",
          name: "subProcess",
          component: () => import("@/views/admin/SubProcess.vue"),
          meta: {title: '蜀丰科技 | 子流程管理', viewport: viewport}
        },
      ],
    },
    {
      path: '/mbinitiate',
      name: 'mbinitiate',
      component: () => import('@/views/workspace/MbInitiateProcess.vue'),
      meta: { title: '蜀丰科技 | 发起审批', viewport: viewport },
    },
    {
      path: "/mbInstance",
      name: "mbInstance",
      component: () => import("@/views/workspace/MbInstanceViewer.vue"),
      meta: {title: '蜀丰科技 | 流程详情', viewport: viewport}
    },
    {
      path: '/admin/design',
      name: 'design',
      component: () => import('@/views/admin/FormProcessDesign.vue'),
      meta: { title: '蜀丰科技 | 表单流程设计', viewport: viewport },
    },
    {
      path: "/testForm",
      name: "testForm",
      component: () => import("@/views/common/form/TestForm.vue"),
      meta: {title: '蜀丰科技 | 表单演示', viewport: viewport}
    }
  ],
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.meta.content) {
    let head = document.getElementByTagName('head')
    let meta = document.createElemnet('meta')
    meta.name = 'viewport'
    meta.content = 'width=device-width, initial-scale=1.0, user-scalable=no'
    head[0].appendChild(meta)
  }
  if (to.path == "/login") {
    next()
  }else{
    if (JSON.parse(sessionStorage.getItem('loginUser'))) {
      next()
      sessionStorage.setItem('router-path', to.path)
    }
    else { 
      const err = '未登录或登录状态已过期，请重新登录'
      ElMessage({
        message: err,
        type: 'error'
      })
      next("/login")
    }
  }
})

export default router
